/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.iti.jets.gpts.service;

import com.iti.jets.gpts.home.PointHome;
import com.iti.jets.gpts.home.TranServiceHome;
import com.iti.jets.gpts.pojo.Point;
import com.iti.jets.gpts.pojo.TranService;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.hibernate.Hibernate;
import util.HibernateUtil;

/**
 *
 * @author khalifaabdelaziz
 */
@Path("/pointservice")
public class PointService {
 PointHome pointHome;

    public PointService() {
        pointHome = new PointHome(HibernateUtil.getSessionFactory());
    }
 
    @Path("/getallpoints")
    @Produces(MediaType.APPLICATION_JSON)
    @GET
    public JSONArray getAllpoints() throws JSONException {
       
        List<Point> loadAll = pointHome.loadAll();

        JSONArray jsonPoints = new JSONArray();

        for (Point point : loadAll) {
            JSONObject jsonPoint = new JSONObject();
            Hibernate.initialize(point);
            jsonPoint.put("id", point.getId());
            jsonPoint.put("name", point.getName());
            jsonPoint.put("location", point.getLocation());
            jsonPoint.put("stop", point.getIsStop());
            jsonPoints.put(jsonPoint);
        }

        return jsonPoints;
    }
    @Path("/addpoint")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @POST
    public JSONObject addpoint(JSONObject jpoint) throws JSONException {
       Point point= new Point();
       point.setIsStop(jpoint.getBoolean("is_stop"));
       point.setLocation(jpoint.getString("location"));
       point.setName(jpoint.getString("name"));
       
      point = pointHome.pointcreate(point);
        
        JSONObject jSONPoint = new JSONObject();
        jSONPoint.put("id", point.getId());
        jSONPoint.put("name", point.getName());
        jSONPoint.put("location", point.getLocation());
        jSONPoint.put("stop", point.getIsStop());
        
        return jSONPoint;
    }
}
